from Calc import Calc
from data import read, write
from unittest import TestCase
from ddt import ddt, data, unpack

"""单元测试框架下的测试用例"""


@ddt()  # 修饰这个类
class TestCalc(TestCase):
    @data(*read(0))  # 引入二维数组
    @unpack  # 解包数据，对应到下面的测试用例的参数里面
    def testAdd(self, row, a, b, expect):
        add = Calc()
        result = add.add(a, b)
        self.assertEqual(expect, result)  # 判断括号中两个值是否相等，不相等的话会报错，提示在哪一行，什么数据
        write(0, row, 4, result)
        if expect == result:
            write(0, row, 5, "pass")
        else:
            write(0, row, 5, "fail")

    @data(*read(1))  # 引入第二个表的二维数组
    @unpack  # 解包数据，对应到下面的测试用例的参数里面
    def testSub(self, row, a, b, expect):
        sub = Calc()
        result = sub.sub(a, b)
        self.assertEqual(expect, result)
        write(1, row, 4, result)
        if expect == result:
            write(1, row, 5, "pass")
        else:
            write((1, row, 5, "fail"))

    @data(*read(2))
    @unpack
    def testMul(self, row, a, b, expect):
        mul = Calc()
        result = mul.mul(a, b)
        write(2, row, 4, result)
        self.assertEqual(expect, result)
        if expect == result:
            write(2, row, 5, "pass")
        else:
            write(2, row, 5, "fail")

    @data(*read(3))
    @unpack
    def testDiv(self, row, a, b, expect):
        div = Calc()
        result = div.div(a, b)
        write(3, row, 4, result)
        self.assertEqual(expect, result)
        if expect == result:
            write(3, row, 5, "pass")
        else:
            write(3, row, 5, "fail")



# """非单元测试框架下的测试用例"""
# add_data = read(0)
# print(add_data)
# for i in add_data:
#     result = Calc().add(i[1],i[2])
#     write(0, i[0], 4, result)
#     if result==i[3]:
#         write(0,i[0],5,"pass")
#     else:
#         write(0, i[0], 5, "fail")